/**
* Copyright (c) 2012-2013 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners. 
* See LICENSE.TXT for license information.
*/

package com.nokia.example.explonoid.game;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.game.LayerManager;

/**
 * Holds the Brick objects and manages their state.
 * <P>
 * For usage, see the Game class.
 * 
 * @see com.nokia.example.explonoid.game.Game
 */
public class BrickGrid {

    private static final int ROWS = 6;
    private static final int COLUMNS = 9;
    private static final int TILE_WIDTH = 22;
    private static final int TILE_HEIGHT = 14;
    private final int LEFT_X;
    private final int TOP_Y;
    private int count = 0;
    int displayWidth;
    int displayHeight;
    private Brick[][] bricks;
    private Resources r;

    public BrickGrid(LayerManager lm, int displayWidth, int displayHeight, Resources resources) {
        this.r = resources;
        this.bricks = new Brick[ROWS][COLUMNS];
        this.displayWidth = displayWidth;
        this.displayHeight = displayHeight;
        LEFT_X = r.scale(18);
        TOP_Y = r.scale(24);
    }

    public Brick collidesWith(Ball ball) {
        Brick brick;
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLUMNS; j++) {
                brick = bricks[i][j];
                if (brick != null && bricks[i][j].collidesWith(ball, false)) {
                    if (brick.getFrame() % 2 == 1) {
                        bricks[i][j] = null;
                        count--;
                    }
                    brick.nextFrame();
                    return brick;
                }
            }
        }
        return null;
    }

    /**
     * Create level bricks accroding to the given layout map
     */
    public void load(LayerManager lm, int[][] layout) {
        Brick brick;
        count = 0;
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLUMNS; j++) {
                brick = bricks[i][j];
                if (brick != null) {
                    lm.remove(brick);
                    bricks[i][j] = null;
                }
                if (layout[i][j] > 0) {
                    brick = new Brick(layout[i][j] - 1, r);
                    brick.setPosition(LEFT_X + r.scale(j * TILE_WIDTH), TOP_Y + r.scale(i * TILE_HEIGHT));
                    bricks[i][j] = brick;
                    lm.append(brick);
                    count++;
                }
            }
        }
    }

    /**
     * Save current state of the bricks
     */
    public void writeTo(DataOutputStream dout) throws IOException {
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLUMNS; j++) {
                Brick brick = bricks[i][j];
                if (brick != null) {
                    dout.writeInt(brick.getFrame() + 1);
                }
                else {
                    dout.writeInt(0);
                }
            }
        }
    }

    /**
     * Load the states of the bricks
     */
    public int[][] readFrom(DataInputStream din) throws IOException {
        int[][] layout = new int[ROWS][COLUMNS];
        count = 0;
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLUMNS; j++) {
                layout[i][j] = din.readInt();
                if (layout[i][j] > 0) {
                    count++;
                }
            }
        }
        return layout;
    }

    public boolean isEmpty() {
        return count == 0;
    }
}
